<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>街道操作</title>
    <script src="../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <style type="text/css">
        body {
            padding: 10px;
        }
    </style>

</head>
<body>
<form class="layui-form" action="">
    <input type="hidden" id="streetId" name="streetId">
    <div class="layui-form-item">

        <div class="layui-form-item">
            <label class="layui-form-label">区域选择</label>
            <div class="layui-input-inline">
                <select name="sectorId" id="sectorId" lay-filter="level">
                    <option value="">请选择区域</option>
                    <option value="浙江" selected="">浙江省</option>
                    <option value="你的工号">江西省</option>
                    <option value="你最喜欢的老师">福建省</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">街道名称</label>
            <div class="layui-input-inline">
                <input type="text" name="streetName" id="streetName" lay-verify="required" placeholder="请输入"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1" id="sub">立即提交</button>
        </div>
    </div>
</form>
<script type="text/javascript">

</script>
<script>
    layui.use(['form', 'layedit', 'laydate','layer','element'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , element = layui.element;

        //监听提交
        form.on('submit(demo1)', function (data) {
            var url = "../../streetAction/addOrUpdate";

            $.post(url, data.field, function (obj) {
                if (obj.num == 1) {
                    window.parent.location.reload();//刷新父页面
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }
            }, "json")
            return false;
        });

        // 查询所有区域
        var url = "../../sectorAction/selectAll";
        $.post(url, null, function (obj) {
            $("#sectorId").html('<option value="" selected="selected">请选择区域</option>');
            $.each(obj, function (index, item) {
                $("#sectorId").append('<option value="' + item.sectorId + '">' + item.sectorName + '</option>')
            })
            form.render();
        }, "json")

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
        });


        // 修改赋值
        var streetId = getQueryString("streetId");
        if (streetId) {
            $("#streetId").val(streetId);
            var url = "../../streetAction/findById";
            var data = {"id": streetId};
            $.post(url, data, function (obj) {
                $("#streetName").val(obj.streetName);// 区域名称
                $("#sectorId").val(obj.sectorId);
                form.render();
            }, "json")
        }
        var show=getQueryString("show");
        if(show==1){
            document.getElementById('streetName').setAttribute('disabled', 'disabled');
            document.getElementById('sectorId').setAttribute('disabled', 'disabled')
            $("#sub").hide();
        }

    });
</script>
</body>
</html>